<template>
  <el-dialog :visible.sync="visible" :title="!dataForm.id ? $t('add') : $t('update')" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmitHandle()" :label-width="$i18n.locale === 'en-US' ? '120px' : '80px'">
          <el-form-item label="订单号" prop="orderNo">
          <el-input v-model="dataForm.orderNo" placeholder="订单号"></el-input>
      </el-form-item>
          <el-form-item label="买家id" prop="userId">
          <el-input v-model="dataForm.userId" placeholder="买家id"></el-input>
      </el-form-item>
          <el-form-item label="订单状态  -5订单过期失效-4平台拒绝 -3退款 -2未付款的订单 -1用户取消 0已付款 1已发货 2待发货 3系统确认收货 4平台审核中 5已完成" prop="orderStatus">
          <el-input v-model="dataForm.orderStatus" placeholder="订单状态  -5订单过期失效-4平台拒绝 -3退款 -2未付款的订单 -1用户取消 0已付款 1已发货 2待发货 3系统确认收货 4平台审核中 5已完成"></el-input>
      </el-form-item>
          <el-form-item label="订单是否完结 1完结-1未完结" prop="isClosed">
          <el-input v-model="dataForm.isClosed" placeholder="订单是否完结 1完结-1未完结"></el-input>
      </el-form-item>
          <el-form-item label="是否支付 -1未支付 1已支付" prop="isPay">
          <el-input v-model="dataForm.isPay" placeholder="是否支付 -1未支付 1已支付"></el-input>
      </el-form-item>
          <el-form-item label="商品总金额" prop="goodsMoney">
          <el-input v-model="dataForm.goodsMoney" placeholder="商品总金额"></el-input>
      </el-form-item>
          <el-form-item label="订单总金额 (goodsMoney+deliverMoney 或 realMoney+cash)" prop="totalMoney">
          <el-input v-model="dataForm.totalMoney" placeholder="订单总金额 (goodsMoney+deliverMoney 或 realMoney+cash)"></el-input>
      </el-form-item>
          <el-form-item label="支付方式 1:线下现金支付 2 现金账户支付 3第三方支付4.支付宝.5微信6.银联" prop="payType">
          <el-input v-model="dataForm.payType" placeholder="支付方式 1:线下现金支付 2 现金账户支付 3第三方支付4.支付宝.5微信6.银联"></el-input>
      </el-form-item>
          <el-form-item label="订单备注" prop="orderRemarks">
          <el-input v-model="dataForm.orderRemarks" placeholder="订单备注"></el-input>
      </el-form-item>
          <el-form-item label="订单流水号" prop="orderunique">
          <el-input v-model="dataForm.orderunique" placeholder="订单流水号"></el-input>
      </el-form-item>
          <el-form-item label="付款时间" prop="paymentTime">
          <el-input v-model="dataForm.paymentTime" placeholder="付款时间"></el-input>
      </el-form-item>
          <el-form-item label="发货时间" prop="deliveryTime">
          <el-input v-model="dataForm.deliveryTime" placeholder="发货时间"></el-input>
      </el-form-item>
          <el-form-item label="收货时间" prop="receiveTime">
          <el-input v-model="dataForm.receiveTime" placeholder="收货时间"></el-input>
      </el-form-item>
          <el-form-item label="快递单号" prop="expressNo">
          <el-input v-model="dataForm.expressNo" placeholder="快递单号"></el-input>
      </el-form-item>
          <el-form-item label="快递公司Id" prop="expressId">
          <el-input v-model="dataForm.expressId" placeholder="快递公司Id"></el-input>
      </el-form-item>
          <el-form-item label="收货地址Id" prop="userAddressId">
          <el-input v-model="dataForm.userAddressId" placeholder="收货地址Id"></el-input>
      </el-form-item>
          <el-form-item label="订单类型1.零售2.批发3.易物" prop="orderType">
          <el-input v-model="dataForm.orderType" placeholder="订单类型1.零售2.批发3.易物"></el-input>
      </el-form-item>
          <el-form-item label="下单时间" prop="createTime">
          <el-input v-model="dataForm.createTime" placeholder="下单时间"></el-input>
      </el-form-item>
      </el-form>
    <template slot="footer">
      <el-button @click="visible = false">{{ $t('cancel') }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t('confirm') }}</el-button>
    </template>
  </el-dialog>
</template>

<script>
import debounce from 'lodash/debounce'
export default {
  data () {
    return {
      visible: false,
      dataForm: {
        id: '',
        orderNo: '',
        userId: '',
        orderStatus: '',
        isClosed: '',
        isPay: '',
        goodsMoney: '',
        totalMoney: '',
        payType: '',
        orderRemarks: '',
        orderunique: '',
        paymentTime: '',
        deliveryTime: '',
        receiveTime: '',
        expressNo: '',
        expressId: '',
        userAddressId: '',
        orderType: '',
        createTime: ''
      }
    }
  },
  computed: {
    dataRule () {
      return {
        orderNo: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        userId: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        orderStatus: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        isClosed: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        isPay: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        goodsMoney: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        totalMoney: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        payType: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        orderRemarks: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        orderunique: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        paymentTime: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        deliveryTime: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        receiveTime: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        expressNo: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        expressId: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        userAddressId: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        orderType: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        createTime: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    init () {
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.getInfo()
        }
      })
    },
    // 获取信息
    getInfo () {
      this.$http.get(`/demo/xlsshoporders/${this.dataForm.id}`).then(({ data: res }) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        this.dataForm = {
          ...this.dataForm,
          ...res.data
        }
      }).catch(() => {})
    },
    // 表单提交
    dataFormSubmitHandle: debounce(function () {
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        this.$http[!this.dataForm.id ? 'post' : 'put']('/demo/xlsshoporders/', this.dataForm).then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          }
          this.$message({
            message: this.$t('prompt.success'),
            type: 'success',
            duration: 500,
            onClose: () => {
              this.visible = false
              this.$emit('refreshDataList')
            }
          })
        }).catch(() => {})
      })
    }, 1000, { 'leading': true, 'trailing': false })
  }
}
</script>
